﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using DungChung;
namespace CoVuaServer
{
    class DangKiProcess
    {
        public DangKiProcess(string connectionString)
        {
            dataProvider = new DataProvider(connectionString);
        }
        DataProvider dataProvider;
        public void Start(object obj)
        {
            TcpClient client = (TcpClient)obj;
            Console.WriteLine("Chap nhan ket noi!");
            NetworkStream stream = client.GetStream();
            BinaryReader reader = new BinaryReader(stream);
            BinaryWriter writer = new BinaryWriter(stream);
            TaiKhoanDTO taiKhoan = new TaiKhoanDTO();
            taiKhoan.UserName = reader.ReadString();
            taiKhoan.TenHienThi = reader.ReadString();
            taiKhoan.GioiThieu = reader.ReadString();
            taiKhoan.Password = reader.ReadString();
            taiKhoan.GioiTinh = reader.ReadString();
            taiKhoan.NgaySinh = Convert.ToDateTime(reader.ReadString());
            Console.WriteLine(taiKhoan.Password);
            TaiKhoanDAO taiKhoanDao = new TaiKhoanDAO(dataProvider);
            if (taiKhoanDao.GetAllTaiKhoan("select * from TAIKHOAN where UserName = '" + taiKhoan.UserName + "'").Count == 0)
            {
                writer.Write("OK");
                taiKhoanDao.AddTaiKhoanMoi(taiKhoan);
            }
            else
            {
                writer.Write("Failed");
            }
            client.Close();
        }
    }
}
